package com.lhwl.it.yhsb.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lhwl.it.sb.service.SbService;
import com.lhwl.it.util.PageBean;
import com.lhwl.it.util.RequestMap;
import com.lhwl.it.yhsb.service.YgsbService;

@Controller
@RequestMapping("/itmanager/ygsb")
public class YgsbAction {

	private Log log=LogFactory.getLog(this.getClass());
	@Resource
	private YgsbService ygsbService;
	@Resource
	private SbService sbService;
	
	@RequestMapping()
	public String index(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		return "/WEB-INF/jsp/yhsb/ygsb";
	}
	
	@RequestMapping(value = "/json", method = RequestMethod.POST)
	public String sbJson(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new RequestMap(request);
		HashMap mp=new HashMap();
		PageBean pageBean = this.ygsbService.getPage("YgsbMapper.list", map);
		List list = pageBean.getList();
		request.setAttribute("smap", map);
		JSONArray jsonArray = JSONArray.fromObject(list);
		JSONObject json = new JSONObject();
		json.put("total", pageBean.getRowscount());
		json.put("rows", jsonArray);
		response.getWriter().print(json);
		return null;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deleteYg(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new RequestMap(request);
		String id = map.get("ids").toString();
		String[] ids = id.split(",");
		try {
			for (int i = 0; i < ids.length; i++) {
				map.remove("id");
				map.put("id", ids[i]);
				this.ygsbService.delete("YgsbMapper.delete", map);
			}
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		try {
			this.ygsbService.insert("YgsbMapper.insert", map);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String editZt(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap map = new RequestMap(request);
		HashMap mp=new HashMap();
		mp.put("id", map.get("sbid").toString());
		mp.put("zt", map.get("zt").toString());
		HashMap m=new HashMap();
		m.put("id", map.get("id").toString());
		m.put("glsj", map.get("glsj").toString());
		try {
			this.sbService.update("SbMapper.editZt", mp);
			this.ygsbService.update("YgsbMapper.edit", m);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().print("error");
		}
		return null;
	}
	
	
}
